﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class WeiXinLog
    {
        public int Add(Chain.Model.WeiXinLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into WeiXinLog(");
            builder.Append("MemWeiXinCard,RecordContent,RecordContentType,StatusCode,RandomCode,ErrorTimes,WeiXinLogCreateTime)");
            builder.Append(" values (");
            builder.Append("@MemWeiXinCard,@RecordContent,@RecordContentType,@StatusCode,@RandomCode,@ErrorTimes,@WeiXinLogCreateTime)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemWeiXinCard", SqlDbType.NVarChar, 50), new SqlParameter("@RecordContent", SqlDbType.NVarChar, 50), new SqlParameter("@RecordContentType", SqlDbType.Int, 4), new SqlParameter("@StatusCode", SqlDbType.NVarChar, 50), new SqlParameter("@RandomCode", SqlDbType.NVarChar, 50), new SqlParameter("@ErrorTimes", SqlDbType.Int, 4), new SqlParameter("@WeiXinLogCreateTime", SqlDbType.DateTime) };
            cmdParms[0].Value = model.MemWeiXinCard;
            cmdParms[1].Value = model.RecordContent;
            cmdParms[2].Value = model.RecordContentType;
            cmdParms[3].Value = model.StatusCode;
            cmdParms[4].Value = model.RandomCode;
            cmdParms[5].Value = model.ErrorTimes;
            cmdParms[6].Value = model.WeiXinLogCreateTime;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int WeiXinLogID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from WeiXinLog ");
            builder.Append(" where WeiXinLogID=@WeiXinLogID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@WeiXinLogID", SqlDbType.Int, 4) };
            cmdParms[0].Value = WeiXinLogID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool Delete(string MemWeiXinCard)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from WeiXinLog ");
            builder.Append(" where MemWeiXinCard=@MemWeiXinCard");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemWeiXinCard", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = MemWeiXinCard;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string WeiXinLogIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from WeiXinLog ");
            builder.Append(" where WeiXinLogID in (" + WeiXinLogIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int WeiXinLogID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from WeiXinLog");
            builder.Append(" where WeiXinLogID=@WeiXinLogID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@WeiXinLogID", SqlDbType.Int, 4) };
            cmdParms[0].Value = WeiXinLogID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select WeiXinLogID,MemWeiXinCard,RecordContent,RecordContentType,StatusCode,RandomCode,ErrorTimes,WeiXinLogCreateTime ");
            builder.Append(" FROM WeiXinLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" WeiXinLogID,MemWeiXinCard,RecordContent,RecordContentType,StatusCode,RandomCode,ErrorTimes,WeiXinLogCreateTime ");
            builder.Append(" FROM WeiXinLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.WeiXinLogID desc");
            }
            builder.Append(")AS Row, T.*  from WeiXinLog T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public Chain.Model.WeiXinLog GetModel(int WeiXinLogID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 WeiXinLogID,MemWeiXinCard,RecordContent,RecordContentType,StatusCode,RandomCode,ErrorTimes,WeiXinLogCreateTime from WeiXinLog ");
            builder.Append(" where WeiXinLogID=@WeiXinLogID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@WeiXinLogID", SqlDbType.Int, 4) };
            cmdParms[0].Value = WeiXinLogID;
            Chain.Model.WeiXinLog log = new Chain.Model.WeiXinLog();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["WeiXinLogID"] != null) && (set.Tables[0].Rows[0]["WeiXinLogID"].ToString() != ""))
            {
                log.WeiXinLogID = int.Parse(set.Tables[0].Rows[0]["WeiXinLogID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MemWeiXinCard"] != null) && (set.Tables[0].Rows[0]["MemWeiXinCard"].ToString() != ""))
            {
                log.MemWeiXinCard = set.Tables[0].Rows[0]["MemWeiXinCard"].ToString();
            }
            if ((set.Tables[0].Rows[0]["RecordContent"] != null) && (set.Tables[0].Rows[0]["RecordContent"].ToString() != ""))
            {
                log.RecordContent = set.Tables[0].Rows[0]["RecordContent"].ToString();
            }
            if ((set.Tables[0].Rows[0]["RecordContentType"] != null) && (set.Tables[0].Rows[0]["RecordContentType"].ToString() != ""))
            {
                log.RecordContentType = int.Parse(set.Tables[0].Rows[0]["RecordContentType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StatusCode"] != null) && (set.Tables[0].Rows[0]["StatusCode"].ToString() != ""))
            {
                log.StatusCode = set.Tables[0].Rows[0]["StatusCode"].ToString();
            }
            if ((set.Tables[0].Rows[0]["RandomCode"] != null) && (set.Tables[0].Rows[0]["RandomCode"].ToString() != ""))
            {
                log.RandomCode = set.Tables[0].Rows[0]["RandomCode"].ToString();
            }
            if ((set.Tables[0].Rows[0]["ErrorTimes"] != null) && (set.Tables[0].Rows[0]["ErrorTimes"].ToString() != ""))
            {
                log.ErrorTimes = int.Parse(set.Tables[0].Rows[0]["ErrorTimes"].ToString());
            }
            if ((set.Tables[0].Rows[0]["WeiXinLogCreateTime"] != null) && (set.Tables[0].Rows[0]["WeiXinLogCreateTime"].ToString() != ""))
            {
                log.WeiXinLogCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["WeiXinLogCreateTime"].ToString());
            }
            return log;
        }

        public Chain.Model.WeiXinLog GetModel(string MemWeiXinCard)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 WeiXinLogID,MemWeiXinCard,RecordContent,RecordContentType,StatusCode,RandomCode,ErrorTimes,WeiXinLogCreateTime from WeiXinLog ");
            builder.Append(" where MemWeiXinCard=@MemWeiXinCard");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemWeiXinCard", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = MemWeiXinCard;
            Chain.Model.WeiXinLog log = new Chain.Model.WeiXinLog();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["WeiXinLogID"] != null) && (set.Tables[0].Rows[0]["WeiXinLogID"].ToString() != ""))
            {
                log.WeiXinLogID = int.Parse(set.Tables[0].Rows[0]["WeiXinLogID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MemWeiXinCard"] != null) && (set.Tables[0].Rows[0]["MemWeiXinCard"].ToString() != ""))
            {
                log.MemWeiXinCard = set.Tables[0].Rows[0]["MemWeiXinCard"].ToString();
            }
            if ((set.Tables[0].Rows[0]["RecordContent"] != null) && (set.Tables[0].Rows[0]["RecordContent"].ToString() != ""))
            {
                log.RecordContent = set.Tables[0].Rows[0]["RecordContent"].ToString();
            }
            if ((set.Tables[0].Rows[0]["RecordContentType"] != null) && (set.Tables[0].Rows[0]["RecordContentType"].ToString() != ""))
            {
                log.RecordContentType = int.Parse(set.Tables[0].Rows[0]["RecordContentType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StatusCode"] != null) && (set.Tables[0].Rows[0]["StatusCode"].ToString() != ""))
            {
                log.StatusCode = set.Tables[0].Rows[0]["StatusCode"].ToString();
            }
            if ((set.Tables[0].Rows[0]["RandomCode"] != null) && (set.Tables[0].Rows[0]["RandomCode"].ToString() != ""))
            {
                log.RandomCode = set.Tables[0].Rows[0]["RandomCode"].ToString();
            }
            if ((set.Tables[0].Rows[0]["ErrorTimes"] != null) && (set.Tables[0].Rows[0]["ErrorTimes"].ToString() != ""))
            {
                log.ErrorTimes = int.Parse(set.Tables[0].Rows[0]["ErrorTimes"].ToString());
            }
            if ((set.Tables[0].Rows[0]["WeiXinLogCreateTime"] != null) && (set.Tables[0].Rows[0]["WeiXinLogCreateTime"].ToString() != ""))
            {
                log.WeiXinLogCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["WeiXinLogCreateTime"].ToString());
            }
            return log;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM WeiXinLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.WeiXinLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update WeiXinLog set ");
            builder.Append("MemWeiXinCard=@MemWeiXinCard,");
            builder.Append("RecordContent=@RecordContent,");
            builder.Append("RecordContentType=@RecordContentType,");
            builder.Append("StatusCode=@StatusCode,");
            builder.Append("RandomCode=@RandomCode,");
            builder.Append("ErrorTimes=@ErrorTimes,");
            builder.Append("WeiXinLogCreateTime=@WeiXinLogCreateTime");
            builder.Append(" where WeiXinLogID=@WeiXinLogID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemWeiXinCard", SqlDbType.NVarChar, 50), new SqlParameter("@RecordContent", SqlDbType.NVarChar, 50), new SqlParameter("@RecordContentType", SqlDbType.Int, 4), new SqlParameter("@StatusCode", SqlDbType.NVarChar, 50), new SqlParameter("@RandomCode", SqlDbType.NVarChar, 50), new SqlParameter("@ErrorTimes", SqlDbType.Int, 4), new SqlParameter("@WeiXinLogCreateTime", SqlDbType.DateTime), new SqlParameter("@WeiXinLogID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.MemWeiXinCard;
            cmdParms[1].Value = model.RecordContent;
            cmdParms[2].Value = model.RecordContentType;
            cmdParms[3].Value = model.StatusCode;
            cmdParms[4].Value = model.RandomCode;
            cmdParms[5].Value = model.ErrorTimes;
            cmdParms[6].Value = model.WeiXinLogCreateTime;
            cmdParms[7].Value = model.WeiXinLogID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

